package com.qf.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.HousePojoMapper;
import com.qf.dto.HouseDTO;
import com.qf.dto.PageDTO;
import com.qf.pojo.HousePojo;
import com.qf.pojo.HousePojoExample;
import com.qf.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HousePojoMapper mapper;
    public int addHouse(HouseDTO dto) throws ParseException {
        HousePojo pojo = new HousePojo();
        pojo.setHousecode(dto.getHouseCode());
        String bookTime = dto.getBookTime();
        if (!StringUtils.isEmpty(bookTime)){
            pojo.setBooktime(DateUtils.stringToDate(dto.getBookTime()));
        }
        pojo.setInarea(dto.getInArea());
        pojo.setHousetype(dto.getHouseType());
        pojo.setStructure(dto.getStructure());
        pojo.setAddress(dto.getAddress());
        pojo.setDirection(dto.getDirection());
        pojo.setAcreage(dto.getAcreage());
        pojo.setFloorno(dto.getFloorNo());
        pojo.setAllfloor(dto.getAllFloor());
        String hirePrice = dto.getHirePrice();
        if (!StringUtils.isEmpty(hirePrice)) {
            pojo.setHireprice(Double.parseDouble(hirePrice));
        }
        pojo.setHousething(dto.getHouseThing());
        pojo.setMsgresource(dto.getMsgResource());
        pojo.setLinkman(dto.getLinkman());
        pojo.setLinktel(dto.getLinkTel());
        pojo.setEmail(dto.getEmail());
        pojo.setIdentitycard(dto.getIdentityCard());
        pojo.setHouseage(dto.getHouseAge());
        int i = mapper.insertSelective(pojo);
        return i;
    }

    //分页查询功能
    public List<HouseDTO> findHouByPage(PageDTO pageDTO) {
        /*查询前进行分页拦截*/
        PageHelper.startPage(pageDTO.getCurrentPage(),pageDTO.getSize());
        HousePojoExample example = new HousePojoExample();
        List<HouseDTO> dtos = new ArrayList<>();
        List<HousePojo> pojos = mapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(pojos);
        for (HousePojo pojo : pojos) {
            HouseDTO dto = new HouseDTO();
            dto.setId(pojo.getId());
            dto.setHouseCode(pojo.getHousecode());
            dto.setBookTime(DateUtils.dateToString(pojo.getBooktime()));
            dto.setInArea(pojo.getInarea());
            dto.setHouseType(pojo.getHousetype());
            dto.setStructure(pojo.getStructure());
            dto.setAddress(pojo.getAddress());
            dto.setDirection(pojo.getDirection());
            dto.setAcreage(pojo.getAcreage());
            dto.setFloorNo(pojo.getFloorno());
            dto.setAllFloor(pojo.getAllfloor());
            dto.setHirePrice(pojo.getHireprice().toString());
            dto.setHouseThing(pojo.getHousething());
            dto.setMsgResource(pojo.getMsgresource());
            dto.setLinkman(pojo.getLinkman());
            dto.setLinkTel(pojo.getLinktel());
            dto.setEmail(pojo.getEmail());
            dto.setIdentityCard(pojo.getIdentitycard());
            dtos.add(dto);
        }
        pageDTO.setMaxPage(pageInfo.getPages());
        pageDTO.setTotalSizes(pageInfo.getTotal());
        return dtos;
    }

    //删除功能
    public int delHou(Long id) {
        int i = mapper.deleteByPrimaryKey(id);
        return i;
    }

    //根据id进行查找
    public HouseDTO findHouById(Long id) {
        HousePojo pojo = mapper.selectByPrimaryKey(id);
        HouseDTO dto = new HouseDTO();
        dto.setId(pojo.getId());
        dto.setHouseCode(pojo.getHousecode());
        dto.setHouseAge(pojo.getHouseage());
        dto.setBookTime(DateUtils.dateToString(pojo.getBooktime()));
        dto.setInArea(pojo.getInarea());
        dto.setHouseType(pojo.getHousetype());
        dto.setStructure(pojo.getStructure());
        dto.setAddress(pojo.getAddress());
        dto.setDirection(pojo.getDirection());
        dto.setAcreage(pojo.getAcreage());
        dto.setFloorNo(pojo.getFloorno());
        dto.setAllFloor(pojo.getAllfloor());
        dto.setHirePrice(pojo.getHireprice().toString());
        dto.setHouseThing(pojo.getHousething());
        dto.setMsgResource(pojo.getMsgresource());
        dto.setLinkman(pojo.getLinkman());
        dto.setLinkTel(pojo.getLinktel());
        dto.setEmail(pojo.getEmail());
        dto.setIdentityCard(pojo.getIdentitycard());
        return dto;
    }

    //更新功能
    public void updateHou(HouseDTO dto) throws ParseException {
        HousePojo pojo = new HousePojo();
        pojo.setId(dto.getId());
        pojo.setHousecode(dto.getHouseCode());
        String bookTime = dto.getBookTime();
        if (!StringUtils.isEmpty(bookTime)){
            pojo.setBooktime(DateUtils.stringToDate(dto.getBookTime()));
        }
        pojo.setInarea(dto.getInArea());
        pojo.setHousetype(dto.getHouseType());
        pojo.setStructure(dto.getStructure());
        pojo.setAddress(dto.getAddress());
        pojo.setDirection(dto.getDirection());
        pojo.setAcreage(dto.getAcreage());
        pojo.setFloorno(dto.getFloorNo());
        pojo.setAllfloor(dto.getAllFloor());
        String hirePrice = dto.getHirePrice();
        if (!StringUtils.isEmpty(hirePrice)) {
            pojo.setHireprice(Double.parseDouble(hirePrice));
        }
        pojo.setHousething(dto.getHouseThing());
        pojo.setMsgresource(dto.getMsgResource());
        pojo.setLinkman(dto.getLinkman());
        pojo.setLinktel(dto.getLinkTel());
        pojo.setEmail(dto.getEmail());
        pojo.setIdentitycard(dto.getIdentityCard());
        pojo.setHouseage(dto.getHouseAge());
        System.out.println("服务层===================="+pojo);
        mapper.updateByPrimaryKeySelective(pojo);
    }
}
